package cn.itsource.ymcc.util;

import cn.itsource.ymcc.dto.UserDetailsDto;
import cn.itsource.ymcc.exception.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class SecurityContextUtil {

    public static UserDetailsDto getUserDetail(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info("token 解析获取用户信息： ",authentication);
        String principal = (String)authentication.getPrincipal();
        if(principal==null){
            throw new GlobalException("权限校验失败！");
        }
        UserDetailsDto detaiDto = JSON.parseObject(principal, UserDetailsDto.class);

        return detaiDto;
    }
}
